﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.utils
{
    public class DragMoveUtils
    {
        public DragMoveUtils(MouseButtons MouseButton)
        {
            this.MouseButton = MouseButton;
        }
        public DragMoveUtils()
        {
            AllMouseButton = true;
        }

        object AboutControl;                                  // 表示当前移动控件操作是针对哪个控件的
        bool Moving = false;                                  // 表示是否正在移动控件
        bool AllMouseButton = false;                          // 表示是否处理所有按钮
        Point ControlStartLocation;                           // 表示移动操作时, 控件的初始位置
        Point MouseFirstLocationAboutParent;                  // 表示移动操作时, 鼠标的初始位置
        MouseButtons MouseButton;
        List<Control> AddedControls = new List<Control>();

        void MouseDownEvent(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButton) || AllMouseButton)
            {
                AboutControl = sender;
                Moving = true;
                ControlStartLocation = (sender as Control).Location;
                if (sender is Form)
                {
                    MouseFirstLocationAboutParent = Control.MousePosition;
                }
                else
                {
                    MouseFirstLocationAboutParent = (sender as Control).Parent.PointToClient(Control.MousePosition);
                }
            }
        }
        void MouseMoveEvent(object sender, MouseEventArgs e)
        {
            if (sender == AboutControl)
            {
                if (Moving)
                {
                    Point MouseLocationNow;
                    if (sender is Form)
                    {
                        MouseLocationNow = Control.MousePosition;
                    }
                    else
                    {
                        MouseLocationNow = (sender as Control).Parent.PointToClient(Control.MousePosition);
                    }
                    Point ControlNewLocation = new Point(MouseLocationNow.X - MouseFirstLocationAboutParent.X + ControlStartLocation.X, MouseLocationNow.Y - MouseFirstLocationAboutParent.Y + ControlStartLocation.Y);
                    (sender as Control).Location = ControlNewLocation;
                }
            }
            else
            {
                if (Moving)
                {
                    Moving = false;
                    (AboutControl as Control).Location = ControlStartLocation;
                }
            }
        }
        void MouseUpEvent(object sender, MouseEventArgs e)
        {
            if (sender != AboutControl)
            {
                if (Moving)
                {
                    (AboutControl as Control).Location = ControlStartLocation;
                }
            }
            Moving = false;
        }
        public bool AddControl(Control control)
        {
            if (AddedControls.Contains(control))
            {
                return false;
            }
            else
            {
                control.MouseDown += MouseDownEvent;
                control.MouseMove += MouseMoveEvent;
                control.MouseUp += MouseUpEvent;
                AddedControls.Add(control);
                return true;
            }
        }
        public bool RemoveControl(Control control)
        {
            if (AddedControls.Contains(control))
            {
                control.MouseDown -= MouseDownEvent;
                control.MouseMove -= MouseMoveEvent;
                control.MouseUp -= MouseUpEvent;
                AddedControls.Remove(control);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
